/*
 * @Author       : LuHeQiu
 * @Date         : 2023-04-21 20:23:27
 * @LastEditTime : 2023-04-27 21:43:14
 * @LastEditors  : LuHeQiu
 * @Description  : 
 * @FilePath     : /UnionLink-Ctrl/Application/utils.c
 * @HomePage     : https://www.luheqiu.com
 */
#include "utils.h"
#include "main.h"
#include "crc.h"


/**
 @brief 获取5字节唯一MAC地址
 @note  5字节与4字节MAC地址的区别在于5字节补充了一个修正最低位。
 @param pMac - [out] MAC地址
 @return 无
*/
void Get5BytesMacAddress(uint8_t *pMac)
{
    uint32_t uid[3] = {HAL_GetUIDw2(), HAL_GetUIDw1(), HAL_GetUIDw0()};
    
    uint32_t fixid = uid[0] ^ uid[1] ^ uid[2];

    uint32_t mapid = HAL_CRC_Calculate(&hcrc, (uint32_t *)uid, 3);

    pMac[0] = fixid & 0xFF;
    pMac[1] = (uint8_t)(mapid & 0xFF);
    pMac[2] = (uint8_t)((mapid & 0xFF00) >> 8);
    pMac[3] = (uint8_t)((mapid & 0xFF0000) >> 16);
    pMac[4] = (uint8_t)((mapid & 0xFF000000) >> 24);
    
}


/**
 @brief 获取4字节唯一MAC地址
 @note  4字节与5字节MAC地址的区别在于5字节补充了一个修正最低位。
 @param pMac - [out] MAC地址
 @return 无
*/
uint32_t Get4BytesMacAddress(void)
{
    uint32_t uid[3] = {HAL_GetUIDw2(), HAL_GetUIDw1(), HAL_GetUIDw0()};

    uint32_t mapid = HAL_CRC_Calculate(&hcrc, (uint32_t *)uid, 3);

    return mapid;
}


